<?php
namespace addons\xykeep\listener;

use app\api\model\xykeep\User;
use app\api\model\xykeep\User as UserModel;
use app\common\model\Benfit;
use app\common\model\UserCard;
use app\common\model\UserCardBenfit;
use app\common\model\BenfitLog;
use app\api\model\xykeep\goods\OrderItem;
use think\Queue;

class Order
{
    public function xykeepOrderPayedAfter(&$params){
        $order = $params['order'];
        $orderType = $params['type'];


        //消息通知
        if($orderType == 'course' || $orderType == 'goods'){

            //减少用户权益
            $ext = json_decode($order->ext,true);
            if(isset($ext['benfit_num']) && $ext['benfit_num']>0){
                $UserCardBenfitModel = new UserCardBenfit();

                if($orderType=='course'){
                    $use_benfit_ids = Benfit::where([
                        'course_num' => ['gt',0]
                    ])->column('id');
                }else{
                    $order_items = OrderItem::where([
                        'goods_order_id' => $order->id,
                    ])->column('goods_id');
                    $use_benfit_ids = Benfit::where([
                        'goods_id' => ['in',$order_items],
                    ])->column('id');
                }

                $user_benfits = $UserCardBenfitModel::where([
                    'user_id' => $order->user_id,
                    'benfit_id' => ['in',$use_benfit_ids],
                    'nums' => ['gt',0]
                ])->order('nums asc')->select();
                $nums = $ext['benfit_num'];
                $benfit_save = [];
                foreach ($user_benfits as $user_benfit) {
                    if ($nums > 0) {
                        if($user_benfit['nums']>=$nums){
                            $benfit_save[] = [
                                'user_card_benfit_id' => $user_benfit['id'],
                                'benfit_id' => $user_benfit['benfit_id'],
                                'user_card_id' => $user_benfit['user_card_id'],
                                'nums' => $nums
                            ];
                            $nums = 0;
                        }else{
                            $benfit_save[] = [
                                'user_card_benfit_id' => $user_benfit['id'],
                                'benfit_id' => $user_benfit['benfit_id'],
                                'user_card_id' => $user_benfit['user_card_id'],
                                'nums' => $user_benfit['nums']
                            ];
                            $nums -= $user_benfit['nums'];
                        }
                    } else if ($nums < 0) {
                        throw \Exception('权益卡内剩余数量不足以抵扣');
                    } else {
                        break;
                    }
                }
                $benfit_log_save = [];
                foreach ($benfit_save as $val) {
                    $UserCardBenfitModel::where(['id'=>$val['user_card_benfit_id']])->setDec('nums',$val['nums']);
                    $card_id = UserCard::where([
                        'id' => $val['user_card_id'],
                        'user_id' => $order['user_id'],
                    ])->value('card_id');
                    $benfit_log_save[] = [
                        'order_sn' => $order['order_sn'],
                        'card_id' => $card_id,
                        'user_card_id' => $val['user_card_id'],
                        'nums' => $val['nums'],
                        'benfit_id' => $val['benfit_id'],
                        'user_card_benfit_id' => $val['user_card_benfit_id'],
                        'user_id' => $order['user_id'],
                        'type' => 'use'
                    ];
                }
                $BenfitLogModel = new BenfitLog();
                $BenfitLogModel->saveAll($benfit_log_save);
            }
        }

    }


    //取消订单&退款
    public function xykeepOrderBackAfter(&$params){
        $order = $params['order'];
        $orderType = $params['type'];

        if($orderType=='card'){
            $userCardList = UserCard::where([
                'user_id' => $order['user_id'],
                'order_sn' =>$order['order_sn'],
            ])->column('id');
            $UserCardBenfitList = UserCardBenfit::where([
                'user_id' => $order['user_id'],
                'user_card_id' => ['in',$userCardList],
            ])->select();
            $benfitLog = [];
            $BenfitLogModel = new BenfitLog();
            foreach ($UserCardBenfitList as $val){
                $benfitLog[] = [
                    'order_sn' => $order['order_sn'],
                    'card_id' => $order['card_id'],
                    'user_card_id' => $val['user_card_id'],
                    'nums' => $val['nums'],
                    'benfit_id' => $val['benfit_id'],
                    'user_card_benfit_id' => $val['id'],
                    'user_id' => $val['user_id'],
                    'type' => 'back'
                ];
            }
            $BenfitLogModel->saveAll($benfitLog);

            UserCardBenfit::where([
                'user_id' => $order['user_id'],
                'user_card_id' => ['in',$userCardList],
            ])->delete();
            UserCard::where([
                'user_id' => $order['user_id'],
                'order_sn' =>$order['order_sn'],
            ])->delete();
        }elseif($orderType=='course' || $orderType=='goods'){
            $BenfitLogModel = new BenfitLog();
            $before_log = $BenfitLogModel->where([
                'order_sn' => $order['order_sn'],
                'type' => 'use'
            ])->find();
            if($before_log){
                $BenfitLogModel->save([
                    'order_sn' => $order['order_sn'],
                    'card_id' => $before_log['card_id'],
                    'user_card_id' => $before_log['user_card_id'],
                    'nums' => $before_log['nums'],
                    'benfit_id' => $before_log['benfit_id'],
                    'user_card_benfit_id' => $before_log['user_card_benfit_id'],
                    'user_id' => $before_log['user_id'],
                    'type' => 'back'
                ]);

                UserCardBenfit::where([
                    'user_id' => $order['user_id'],
                    'id' => $before_log['user_card_benfit_id'],
                ])->setInc('nums',$before_log['nums']);

            }
        }

    }

}